﻿Public Class MainMDIHelper
    Implements IDisposable

    Private _mainMDI As Form
    Private _dockManager As XtraBars.Docking.DockManager
    Private _mdiManager As XtraTabbedMdi.XtraTabbedMdiManager

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    End Sub


    Public Sub New(aMDI As Form, dockManager As XtraBars.Docking.DockManager, mdiManager As XtraTabbedMdi.XtraTabbedMdiManager)
        ' This call is required by the designer.
        InitializeComponent()
        _mainMDI = aMDI
        _dockManager = dockManager
        _mdiManager = mdiManager
    End Sub


    Public Function FormIsOpen(ByVal frmName As String) As Boolean
        For Each aFrm As Form In _mainMDI.MdiChildren
            If aFrm.Name = frmName Then
                Return True
            End If
        Next

        Return False
    End Function

    Public Overridable Sub MNU_FRM_LoadExplorer(frmCD As String, caption As String, ByVal img As System.Drawing.Image, typeOfpl As PLTypeOf, typeOFwinCrtl As Win.WinCtrlTypeOf, MetaQueryCD As String)
        ' cette verification est inutile tant que les menu ne sont pas afficher sans une connection valide.
        ' If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then



        'Else
        'If MessageWindow.ShowMessage(DeskTopManager.GetMessage("MES-000001")) = DialogResult.OK Then
        '    LoadLoginForm()
        'End If
        'End If

        Me.Cursor = Cursors.WaitCursor
        'code goes there
        If Not FormIsOpen(frmCD) Then
            Dim adockpanel As XtraBars.Docking.DockPanel = _dockManager.AddPanel(XtraBars.Docking.DockingStyle.Left)
            adockpanel.FloatVertical = True
            adockpanel.Name = frmCD
            adockpanel.Text = caption
            adockpanel.Width = 500 'looks good

            Dim DockImage As Image = img.Clone
            DockImage.RotateFlip(RotateFlipType.Rotate270FlipNone)
            adockpanel.Image = DockImage
            adockpanel.Visibility = XtraBars.Docking.DockVisibility.AutoHide

            Dim aModuleExplorer As ExplorerTreeList = Win.WinCtrlBase.CreateInstanceOf(typeOFwinCrtl)
            aModuleExplorer.Dock = DockStyle.Fill
            aModuleExplorer.QueryName = MetaQueryCD
            aModuleExplorer.PL = PLBase.CreateInstanceOf(typeOfpl)
            aModuleExplorer.PanelName = frmCD
            aModuleExplorer.KeyFieldName = "KeyID"
            aModuleExplorer.ParentFieldName = "ParentID"

            adockpanel.ControlContainer.Controls.Add(aModuleExplorer)

            Dim newForm As XtraEditors.XtraForm = New XtraEditors.XtraForm() With {.Name = frmCD, .Text = caption, .Icon = ImageExt.ToIcon(img, 16, True), .ShowIcon = True}
            AddHandler newForm.FormClosing, AddressOf MDIForm_Closing
            newForm.MdiParent = _mainMDI
        Else
            If _dockManager.Panels(frmCD).Visibility = XtraBars.Docking.DockVisibility.Hidden Then
                _dockManager.Panels(frmCD).Visibility = XtraBars.Docking.DockVisibility.AutoHide
            End If
        End If



        Me.Cursor = Cursors.Default


    End Sub


    Public Overridable Sub MNU_FRM_LoadGrid(frmCD As String, caption As String, img As System.Drawing.Image, typeOfpl As PLTypeOf, MetaQueryCD As String, Optional ByVal toto As WinFormExt.GridExtPL.InitGridActionOptionListEventHandler = Nothing)
        Me.Cursor = Cursors.WaitCursor

        Dim newForm As XtraEditors.XtraForm = New XtraEditors.XtraForm() With {.Name = frmCD, .Text = caption, .Icon = ImageExt.ToIcon(img, 16, True), .ShowIcon = True}


        Dim aGridControl As GridExtPL = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = PLBase.CreateInstanceOf(typeOfpl), .SecureGrid = True}
        aGridControl.Pl.QueryName = MetaQueryCD
        AddHandler aGridControl.InitGridActionOptionList, toto
        AddHandler newForm.FormClosing, AddressOf MDIForm_Closing
        newForm.Controls.Add(aGridControl)
        newForm.MdiParent = _mainMDI
        newForm.Show()

        Me.Cursor = Cursors.Default
    End Sub


    ''' <summary>
    ''' SSPP: Use this Subroutine to close all MDI child
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>All the dispose call is to recover memory</remarks>
    Private Sub MDIForm_Closing(sender As Object, e As FormClosingEventArgs)
        Dim form As XtraForm = CType(sender, XtraForm)
        Dim panel As XtraBars.Docking.DockPanel = _dockManager.Panels(CType(sender, Form).Name)
        Dim dispo As IDisposable

        Dim iSave As ISaveCommand

        ''TestPoint: By design we call dispose on each control that have the dispose Method
        For Each o As Object In form.Controls

            iSave = TryCast(o, ISaveCommand)
            If iSave IsNot Nothing AndAlso iSave.Dataset.HasChanges Then
                If MsgBox(String.Format("Vous avez des modification qui n'ont pas été sauvegarder !{0}Voulez-vous les sauvegarder maintenant ?", vbCrLf), MsgBoxStyle.YesNo, "* ATTENTION *") = Windows.Forms.DialogResult.Yes Then
                    iSave.SaveCommand()
                End If
            End If
            '
            dispo = TryCast(o, IDisposable)
            If dispo IsNot Nothing Then
                dispo.Dispose()
            End If
        Next

        If panel IsNot Nothing Then
            For Each o As Object In panel.Controls
                dispo = TryCast(o, IDisposable)
                If dispo IsNot Nothing Then
                    dispo.Dispose()
                End If
            Next
            _dockManager.RemovePanel(panel)
            panel.Controls.Clear()
            panel.Dispose()
        End If

        RemoveHandler form.FormClosing, AddressOf MDIForm_Closing
        form.Controls.Clear()
        form.Dispose()
        form = Nothing
        panel = Nothing

        If _mdiManager.Pages.Count = 0 Then
            Cache.CacheExt.ClearCache()

            GC.Collect()
            GC.WaitForFullGCComplete()

        End If
    End Sub


    ''' <summary>
    ''' FRM_00021_Init_GridActionOptionList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Sub FRM_00021_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("MetaControlsMaster", "ACT_00047", "Export MetaControls to " + AppManager.ExportToMasterDB, AddressOf MenuExportMetaControlsToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaControls", "ACT_00048", "Export MetaControls", AddressOf MenuExportMetaControls, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaControls", "ACT_00049", "Import MetaControls", AddressOf MenuImportMetaControls, True, True)
        x.GridActionOptionList.Add(action)
    End Sub

    ''' <summary>
    ''' FRM_00023_Init_GridActionOptionList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub FRM_00023_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("MetaListsMaster", "ACT_00039", "Export MetaLists to " + AppManager.ExportToMasterDB, AddressOf MenuExportMetaListsToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaLists", "ACT_00040", "Export MetaLists", AddressOf MenuExportMetaLists, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaLists", "ACT_00041", "Import MetaLists", AddressOf MenuImportMetaLists, True, True)
        x.GridActionOptionList.Add(action)
    End Sub

    ''' <summary>
    ''' FRM_00024_Init_GridActionOptionList Action
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub FRM_00024_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("MetaStringsMaster", "ACT_00033", "Export Meta String to " + AppManager.ExportToMasterDB, AddressOf MenuExportMetaStringsToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaStrings", "ACT_00034", "Export Meta String", AddressOf MenuExportMetaStrings, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaStrings", "ACT_00035", "Import Meta String", AddressOf MenuImportMetaStrings, True, True)
        x.GridActionOptionList.Add(action)
    End Sub

    ''' <summary>
    ''' FRM_00026_Init_GridActionOptionList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub FRM_00026_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("MetaValuesMaster", "ACT_00036", "Export Meta Values to " + AppManager.ExportToMasterDB, AddressOf MenuExportMetaValuesToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaValues", "ACT_00037", "Export Meta Values", AddressOf MenuExportMetaValues, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaValues", "ACT_00038", "Import Meta Values", AddressOf MenuImportMetaValues, True, True)
        x.GridActionOptionList.Add(action)
    End Sub

    ''' <summary>
    ''' FRM00028_Init_GridActionOptionList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub FRM00028_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("SequenceMaster", "ACT_00057", "Export Sequence to " + AppManager.ExportToMasterDB, AddressOf MenuExportSequenceToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Sequence", "ACT_00058", "Export Sequence", AddressOf MenuExportSequence, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Sequence", "ACT_00059", "Import Sequence", AddressOf MenuImportSequence, True, True)
        x.GridActionOptionList.Add(action)
    End Sub

    ''' <summary>
    ''' FRM_00027_Init_GridActionOptionList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Sub FRM_00027_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("ListsMaster", "ACT_00053", "Export Lists to " + AppManager.ExportToMasterDB, AddressOf MenuExportListsToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Lists", "ACT_00054", "Export Lists", AddressOf MenuExportLists, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Lists", "ACT_00055", "Import Lists", AddressOf MenuImportLists, True, True)
        x.GridActionOptionList.Add(action)
    End Sub



    ''' <summary>
    ''' FRM_00043_Init_GridActionOptionList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub FRM_00043_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("MetaUsersMaster", "ACT_00042", "Export MetaUser to " + AppManager.ExportToMasterDB, AddressOf MenuExportMetaUsersToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaUsers", "ACT_00043", "Export MetaUser", AddressOf MenuExportMetaUsers, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaUsers", "ACT_00044", "Import MetaUser", AddressOf MenuImportMetaUsersColumns, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("MetaUsersCopyTo", "ACT_00045", "Copy MetaUser To...", AddressOf MenuCopyMetaUser, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("CreateUser", "ACT_00046", "Create User", AddressOf MenuCreateUser, True, True)
        x.GridActionOptionList.Add(action)
    End Sub

    Public Overridable Sub FRM_00052_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("FormatMaster", "ACT_00060", "Export Format to " + AppManager.ExportToMasterDB, Nothing, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Format", "ACT_00061", "Export Format", Nothing, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Format", "ACT_00062", "Import Format", Nothing, True, True)
        x.GridActionOptionList.Add(action)
    End Sub


    ''' <summary>
    ''' FRM_00091_Init_GridActionOptionList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub FRM_00091_Init_GridActionOptionList(sender As Object, e As EventArgs)
        Dim x As GridExtPL = TryCast(sender, GridExtPL)
        Dim action As ActionOption = New ActionOption("SequenceMaster", "ACT_00057", "Export Sequence to " + AppManager.ExportToMasterDB, AddressOf MenuExportSequenceToMaster, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Sequence", "ACT_00058", "Export Sequence", AddressOf MenuMETA_ExportSequence, True, True)
        x.GridActionOptionList.Add(action)
        action = New ActionOption("Sequence", "ACT_00059", "Import Sequence", AddressOf MenuMETA_ImportSequence, True, True)
        x.GridActionOptionList.Add(action)
    End Sub


#Region "MetaData Action"

    Private Sub MenuExportMetaStrings(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim stringID As Guid
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaString_" + aDataRow("StringCD").ToString
            If aFile.ShowDialog() = DialogResult.OK Then
                stringID = New Guid(aDataRow("StringID").ToString)
                If MetaStrings_PL.ExportMetaStrings(aFile.FileName, stringID) Then
                    MessageWindow.ShowMessage("Export MetaStrings successful", MsgBoxStyle.Information)
                End If
            End If
        End Using

    End Sub

    Private Sub MenuExportMetaStringsToMaster(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim stringID As Guid

        stringID = New Guid(aDataRow("StringID").ToString)
        If MetaStrings_PL.ExportMetaStringsToMaster(stringID) Then
            MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub MenuImportMetaStrings(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "MetaString_*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                For Each file As String In aFile.FileNames
                    saved = MetaStrings_PL.ImportMetaStrings(file)
                Next
                If saved Then
                    MessageWindow.ShowMessage("Import MetaStrings successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportMetaValues(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim metaValueID As Guid
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaValue_" + aDataRow("ValueID").ToString
            If aFile.ShowDialog() = DialogResult.OK Then
                metaValueID = New Guid(aDataRow("MetaValueID").ToString)
                If MetaValues_PL.ExportMetaValues(aFile.FileName, metaValueID) Then
                    MessageWindow.ShowMessage("Export MetaValues successful", MsgBoxStyle.Information)
                End If
            End If
        End Using

    End Sub

    Private Sub MenuExportMetaValuesToMaster(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim metaValueID As Guid

        metaValueID = New Guid(aDataRow("metaValueID").ToString)
        If MetaValues_PL.ExportMetaValuesToMaster(metaValueID) Then
            MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
        End If

    End Sub

    Private Sub MenuImportMetaValues(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "MetaValue_*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                For Each file As String In aFile.FileNames
                    saved = MetaValues_PL.ImportMetaValues(file)
                Next
                If saved Then
                    MessageWindow.ShowMessage("Import MetaValues successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportMetaLists(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim listID As Guid
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaList_" + aDataRow("ListCD").ToString
            If aFile.ShowDialog() = DialogResult.OK Then
                listID = New Guid(aDataRow("ListID").ToString)
                If MetaLists_PL.ExportMetaLists(aFile.FileName, listID) Then
                    MessageWindow.ShowMessage("Export MetaLists successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportMetaListsToMaster(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim listID As Guid

        listID = New Guid(aDataRow("ListID").ToString)
        If MetaLists_PL.ExportMetaListsToMaster(listID) Then
            MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub MenuImportMetaLists(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "MetaList_*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                For Each file As String In aFile.FileNames
                    saved = MetaLists_PL.ImportMetaLists(file)
                Next
                If saved Then
                    MessageWindow.ShowMessage("Import MetaLists successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportLists(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim listID As Guid
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "List_" + aDataRow("ListCD").ToString
            If aFile.ShowDialog() = DialogResult.OK Then
                listID = New Guid(aDataRow("ListID").ToString)
                If Lists_PL.ExportMetaLists(aFile.FileName, listID) Then
                    MessageWindow.ShowMessage("Export Lists successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportListsToMaster(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim listID As Guid

        listID = New Guid(aDataRow("ListID").ToString)
        If Lists_PL.ExportMetaListsToMaster(listID) Then
            MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub MenuImportLists(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "List_*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                For Each file As String In aFile.FileNames
                    saved = SystemBoxSDK.PL.CoreSDK.Lists_PL.ImportMetaLists(file)
                Next
                If saved Then
                    MessageWindow.ShowMessage("Import Lists successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportMetaControls(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim controlID As Guid
        Dim controlCD As String = aDataRow("ControlCD").ToString
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaControl_" + controlCD + "_" + aDataRow("ControlName").ToString
            If aFile.ShowDialog() = DialogResult.OK Then
                controlID = New Guid(aDataRow("ControlID").ToString)
                If MetaControls_PL.ExportMetaControls(aFile.FileName, controlID) Then
                    MessageWindow.ShowMessage("Export MetaControls successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportMetaControlsToMaster(ByVal sender As System.Object, ByVal e As EventArgs)

        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim controlID As Guid

        controlID = New Guid(aDataRow("ControlID").ToString)
        If MetaControls_PL.ExportMetaControlsToMaster(controlID) Then
            MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub MenuExportMetaUsers(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim userID As Guid
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaUser_" + aDataRow("UserName").ToString
            If aFile.ShowDialog() = DialogResult.OK Then
                userID = New Guid(aDataRow("UserID").ToString)
                If SystemBoxSDK.PL.MetaSDK.MetaUsers_PL.ExportMetaUsers(aFile.FileName, userID) Then
                    MessageWindow.ShowMessage("Export Meta User successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub MenuExportMetaUsersToMaster(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim userID As Guid

        userID = New Guid(aDataRow("UserID").ToString)
        If SystemBoxSDK.PL.MetaSDK.MetaUsers_PL.ExportMetaUsersToMaster(userID) Then
            MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub MenuCopyMetaUser(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim newName As String = String.Format("Copy of {0}", aDataRow("UserName"))

        Try
            MetaUsers_PL.CopyMetaUser(CType(aDataRow("UserID"), Guid), newName)
            MessageWindow.ShowMessage(String.Format("Copy of '{0}' To '{1}' Done", aDataRow("UserName"), newName), MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    ''' <summary>
    ''' Add column to gridsp used in Create User 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AddRowParameter(sender As Object, e As System.EventArgs)
        Try
            Dim aGrid As GridExtSP = CType(sender, GridExtSP)

            Dim arow As DataRow = aGrid.MetaDataTable.NewRow()
            'assign value for the metacolumn here

            aGrid.MetaDataTable.Rows.Add(arow)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    ''' <summary>
    ''' Where the save is used when user create
    ''' </summary>
    ''' <param name="aMetaDataTable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overridable Function CreateUser(ByVal aMetaDataTable As MetaDataTable) As Boolean
        Try

            If aMetaDataTable.ApplyChanges(reload:=False) > 0 Then
                MessageWindow.ShowMessage("Successfully Create User.", MsgBoxStyle.Information)
            Else
                MessageWindow.ShowMessage("Problem to create user, please check your value", MsgBoxStyle.Information)
            End If

        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
        Return True
    End Function

    ''' <summary>
    ''' Launch the screen and the SSP for creating user
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MenuCreateUser(ByVal sender As System.Object, ByVal e As EventArgs)

        Try
            Dim selectedRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

            Me.Cursor = Cursors.WaitCursor
            Dim newForm As XtraForm = New XtraForm() With {.Text = "Create User", .Width = 800}
            Dim gridSP As GridExtSP = New GridExtSP("SSPCreateUser") With {.Dock = DockStyle.Fill}
            gridSP.SaveCMD = AddressOf CreateUser

            AddHandler gridSP.AfterLoad, AddressOf AddRowParameter

            newForm.Controls.Add(gridSP)
            newForm.ShowDialog()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Private Sub MenuImportMetaUsersColumns(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                For Each file As String In aFile.FileNames
                    saved = SystemBoxSDK.PL.MetaSDK.MetaUsers_PL.ImportMetaUsers(file)
                Next
                If saved Then
                    MessageWindow.ShowMessage("Import Meta User successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub


    Private Sub MenuImportMetaControls(ByVal sender As System.Object, ByVal e As EventArgs)
        ImportMetaControls()
    End Sub

    Private Sub MenuExportSequence(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim SequenceId As Guid
        Dim SequenceCd As String = aDataRow("Entity").ToString
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "Sequence_" + SequenceCd
            If aFile.ShowDialog() = DialogResult.OK Then
                SequenceId = New Guid(aDataRow("SequenceId").ToString)
                If Sequence_PL.ExportSequence(aFile.FileName, SequenceId) Then
                    MessageWindow.ShowMessage("Export Sequence successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub
    Private Sub MenuExportSequenceToMaster(ByVal sender As System.Object, ByVal e As EventArgs)

        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim SequenceId As Guid = New Guid(aDataRow("SequenceId").ToString)
        If Sequence_PL.ExportSequenceToMaster(SequenceId) Then
            MessageWindow.ShowMessage("Export Sequence Successful", MsgBoxStyle.Information)
        End If
    End Sub
    Private Sub MenuImportSequence(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "Sequence_*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                Me.Cursor = Cursors.WaitCursor
                For Each file As String In aFile.FileNames
                    saved = Sequence_PL.ImportSequence(file)
                Next
                Me.Cursor = Cursors.Default
                If saved Then
                    MessageWindow.ShowMessage("Import Sequence successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub


    Private Sub MenuMETA_ExportSequence(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim SequenceId As Guid
        Dim SequenceCd As String = aDataRow("Entity").ToString
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaSequence_" + SequenceCd
            If aFile.ShowDialog() = DialogResult.OK Then
                SequenceId = New Guid(aDataRow("SequenceId").ToString)
                If MetaSequence_PL.ExportMetaSequence(aFile.FileName, SequenceId) Then
                    MessageWindow.ShowMessage("Export Sequence successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub
    Private Sub MenuMETA_ImportSequence(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "MetaSequence*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                Me.Cursor = Cursors.WaitCursor
                For Each file As String In aFile.FileNames
                    saved = MetaSequence_PL.ImportMetaSequence(file)
                Next
                Me.Cursor = Cursors.Default
                If saved Then
                    MessageWindow.ShowMessage("Import Sequence successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    ''' <summary>
    ''' ImportMetaControls 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ImportMetaControls()
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "MetaControl*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                Me.Cursor = Cursors.WaitCursor
                For Each file As String In aFile.FileNames
                    saved = MetaControls_PL.ImportMetaControls(file)
                Next
                Me.Cursor = Cursors.Default
                If saved Then
                    MessageWindow.ShowMessage("Import MetaControls successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub


#End Region




End Class
